$(document).ready(function() {
    var flag = parseInt($("#flagaction").val());
    var url = $("#url").val();
    if (flag == 2) {
        loadpagevideos(flag + 1, url);
    }
    flag = parseInt($("#flagaction").val());
    if (flag == 3) {
        loadvideos(url, 1);
    }
});

function loadpagevideos(flag, url) {
    var pageget = qs["page"];
    var keyword = qs["keyword"];
    var page = "1";
    if (pageget != null) {
        page = pageget;
    }
    $.ajax({
        url: url + "/search/loadpagesearch",
        type: "POST",
        cache: true,
        async: false,
        data: "keyword=" + keyword + "&page=" + page,
        success: function(dataserver) {
            var data = dataserver.split("*duypc*");
            $("#flagaction").val(flag);
            $("#pagevideos").html(data[0]);
            $("#titlecatalog").html('<h2><a href="javascript:void(0);"><i class="fa fa-search"></i> Từ khóa: ' + data[1] + '.</a></h2>');

        }
    });
}
function loadvideos(url, page) {
    var pageget = qs["page"];
    var keyword = qs["keyword"];
    if (pageget != null) {
        page = pageget;
    }
    $.ajax({
        url: url + "/search/loadvideosearch",
        type: "POST",
        cache: true,
        async: false,
        data: "keyword=" + keyword + "&page=" + page,
        success: function(dataserver) {
            $("#listvideosview").html(dataserver);
        }
    });
}
function pagevideos(page) {
    var urlfull = window.location.href;
    var dataurl = urlfull.split("&");
    window.history.replaceState({}, 'page', dataurl[0] + '&page=' + page);
    $("#pagevideos").find("a").removeClass("activepage");
    $("#page" + page).addClass("activepage");
    var url = $("#url").val();
    loadvideos(url, page);

}

var qs = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
        var p=a[i].split('=', 2);
        if (p.length == 1)
            b[p[0]] = "";
        else
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
})(window.location.search.substr(1).split('&'));